function Sim_Dynamic_Write_Results(gamma, psi, p_t_factor, predictability, unc_or_con)

gamma = 5;
psi = 'inf';
p_t_factor = 0;
predictability = 0;
unc_or_con = 0;
folder = '../Result/';
folder_D3 = folder;
Period = 52; 

% predictability must be 0 because the dynamic simulated solution is for the problem with no predictor.
     if predictability ~= 0
        error('This code is built only for the problem with no predictability !!!')
     end
% unc_or_con should be 0 for unconstrained portfolios or any other value for constrained portfolios.
% We are here only interested in the problem with no consumption:
    if ~strcmp(psi, 'inf')
       error('The buy-and-hold strategy is built only for the problem with no consumption, therefore psi must be equal to inf !!!');
    end

    [sheet_results, file, filename] = select_case(predictability, unc_or_con, folder);
    load(file);
    
    filename_data = strcat(folder_D3,'Data.xlsx');
    sheet = 'Parameters';
    range = strcat('S2:', xlsColNum2Str(xlsColStr2Num({'S'})+ n), num2str(period_beg_row + Period));
    Import_Y_t_1_t = xlsread(filename_data, sheet, range{1})';
    
    range2 = strcat('R2:', xlsColNum2Str(xlsColStr2Num({'R'})), num2str(period_beg_row + Period));
    Dates = xlsread(filename_data, sheet, range2{1});
    Dates = datetime(Dates, 'ConvertFrom', 'excel');
    Dates = datetime(Dates, 'Format', 'dd-MMM-yyyy')
       
    pi_matrix = zeros(m,period_beg_row+Period-1); 
    pi_matrix(:,:) = Import_Y_t_1_t (:,:); 
    
    horizons = [260]; 
    hor=261; 
    
    counter = 0;
    Sheet_D3 = strcat('Weights',num2str(hor-1));
    
    for i = 1:size(pi_matrix,2) 
        pi_t = pi_matrix(:,i); 
        [row_prob, pi_t_adj] = Closest_Probability_Vector_In_Grid(pi_t', probability_grid); 
        for j=1:length(horizons)
            tic
            horizon = horizons(j);
            counter = counter + 1;
            % OUR MODEL RESULTS
            if unc_or_con == 0
                optimal_portfolio_model = General_Solution(gamma, psi, horizon, p_t_factor, pi_t, predictability)/100;
            else
                optimal_portfolio_model = General_Solution_No_SS(gamma, psi, horizon, p_t_factor, pi_t, predictability)/100;
            end
                % Dates
                T0 = table(Dates(counter,:))
                local0 = strcat('A', num2str(counter+1));
                writetable(T0,filename,'Sheet',Sheet_D3,'Range',local0,'WriteVariableName',false);
                
                % Portfolio Weights ( = model = suboptimal)
                T1 = table(optimal_portfolio_model);
                local1 = strcat('B', num2str(counter+1));
                writetable(T1,filename,'Sheet',Sheet_D3,'Range',local1,'WriteVariableName',false);
                        
            disp(['Written Weights ', num2str(counter), ' of ', num2str(size(pi_matrix,2)) ]);
            toc
        end
    end    
end  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    function [sheet, file, filename, folder] = select_case(predictability, unc_or_con, folder)
        if predictability == 0
            filename = strcat(folder, 'D2_Model_No_Predictability.xlsx');
            if unc_or_con == 0
                sheet = 'Dynamic_Unconstrained';
                file = strcat(folder, 'D1_Results_Simulation_No_Predictability.mat');
            else
                sheet = 'Dynamic_Constrained';
                file = strcat(folder, 'D1_Results_Simulation_No_SS_No_Predictability.mat');
            end
        else
           filename = strcat(folder, 'D2_Model_Predictability.xlsx');
            if unc_or_con == 0
                sheet = 'Dynamic_Unconstrained';
                file = strcat(folder, 'D1_Results_Simulation_Predictability.mat');
            else
                sheet = 'Dynamic_Constrained';
                file = strcat(folder, 'D1_Results_Simulation_No_SS_Predictability.mat');
            end
        end
  end